package com.fndsoft.police.xuancheng.ui.adapter;

import java.util.ArrayList;

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import com.fndsoft.police.xuancheng.R;
import com.fndsoft.police.xuancheng.domain.ui.CommonFormGroupDomain;
import com.fndsoft.police.xuancheng.domain.ui.CommonFormRowDomain;
import com.fndsoft.police.xuancheng.ui.adapter.holder.CommonFormRowHolder;
import com.fndsoft.police.xuancheng.util.ViewUtil;

/**
 * 通用扩展列表适配器
 * 
 * @author LiangXiangJu
 * @creationTime 2014-7-19 下午4:07:26
 * @version
 */
public class CommonFormGroupAdapter extends BaseExpandableListAdapter {
	private ArrayList<CommonFormGroupDomain> groupDomains;
	private Object dataModel;
	private boolean isRead;

	public CommonFormGroupAdapter(ArrayList<CommonFormGroupDomain> groupDomains,
			Object dataModel, boolean isRead) {
		this.groupDomains = groupDomains;
		this.dataModel = dataModel;
		this.isRead = isRead;
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		ArrayList<CommonFormRowDomain> rowDomains = groupDomains.get(groupPosition)
				.getRowDomains();
		return rowDomains.get(childPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return groupPosition * 1000 + childPosition;
	}

	@Override
	public View getChildView(final int groupPosition, final int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		CommonFormRowHolder holder = null;
		if (null == convertView) {
			convertView = ViewUtil.buildView(R.layout.common_form_row);
			holder = new CommonFormRowHolder(convertView, dataModel, isRead);
		} else {
			holder = (CommonFormRowHolder) convertView.getTag();
		}
		CommonFormRowDomain domain = groupDomains.get(groupPosition)
				.getRowDomains().get(childPosition);
		holder.fillData(childPosition, domain, convertView);
		return convertView;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return groupDomains.get(groupPosition).getRowDomains().size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		return groupDomains.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		return groupDomains.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition * 1000;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		if (null == convertView) {
			convertView = ViewUtil.buildView(R.layout.common_form_group);
		}
		TextView tvTitle = (TextView) convertView.findViewById(R.id.tvTitle);
		CommonFormGroupDomain domain = groupDomains.get(groupPosition);
		tvTitle.setText(domain.getTitle());
		return convertView;
	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return false;
	}

}
